package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import cn.com.utt.unms.pojo.Order;

@Mapper
public interface OrderMapper {

	@Insert("insert into `orders`(order_id,user_id,product_id,num,price,order_time,pay_method,total_fee,pay_status) values(#{orderId},#{userId},#{productId},#{num},#{price},#{orderTime},#{payMethod},#{totalFee},#{payStatus})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void save(Order Order);

	@Select("select * from orders where order_id=#{orderNo}")
	Order queryOrder(@Param("orderNo") String orderNo);

	@Update("update `orders` set pay_status=#{payStatus},pay_time=#{payTime},pay_account=#{payAccount},trade_no=#{tradeNo} where id=#{id}")
	void updateOrder(Order Order);
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM orders",
	    "WHERE user_id=#{userId} ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM orders",
	    "WHERE user_id=#{userId} ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<Order> queryByCondition(Map<String, Object> conditions);

}